<script setup lang="ts">
import { requiredCheck } from "@/utils/verify";
import { ref, onMounted } from "vue";
import { addData, editData } from "@/api/modules/system/account";
import { cfg_sn_get, cfg_softcheck, cfgSoftUpdate, cfgSetSyscfg } from "@/api/modules/system/params";
import { ElMessage } from "element-plus";

interface Props {
  title?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "新增账号",
  rowData: undefined
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import BaseButton from "@/components/BaseButton/index.vue";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    name: [...requiredCheck("请输入用户名")],
    password: [...requiredCheck("请输入密码")],
    passwordRe: [
      {
        validator: (rule, value, callback) => {
          if (value !== updateFormData.password) {
            callback(new Error("两次输入的密码不一致"));
          } else {
            callback();
          }
        },
        trigger: "blur"
      }
    ]
  },
  defaultData: {
    loginTime: 900
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = Object.values(res);
      console.log(ifaces.value);
    }
    Object.values(res);
  } catch (error) {}
}

const loading = ref(false);

async function submitCb() {
  console.log("files.value", files.value);
  if (!files.value.length) {
    ElMessage.warning("请先上传文件");
    return;
  }
  loading.value = true;
  try {
    await cfgSoftUpdate({
      file: files.value[0].raw
    });
    ElMessage.success("更新成功");
    getCfg();
    loading.value = false;
    showHandle.value = false;
  } catch (error) {
    loading.value = false;
  }
}

async function getCfg() {
  const res1 = await cfg_sn_get();
  const res2 = await cfg_softcheck();
  console.log(res1);
  console.log(res2);
  updateFormData.sn = res1;
  updateFormData.version = res2.version;
}

async function setCfg() {
  await cfgSetSyscfg({
    timeout: updateFormData.loginTime,
    syslogo: ""
  });
  ElMessage.success("更新成功");
  getCfg();
}

onMounted(() => {
  getCfg();
});
const uploadRef = ref();
const files = ref([]);
</script>

<template>
  <el-card>
    <template #header>
      <div class="card-header h-box justify-between">
        <span>系统升级</span>
        <base-button @click="showHandle = true" type="edit_text"></base-button>
      </div>
    </template>

    <el-form style="height: 148px" ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-suffix="：">
      <el-form-item label="版本号"
        ><el-tag style="font-size: 10px" size="small">V1.0-{{ updateFormData.version }}</el-tag></el-form-item
      >
      <el-form-item label="序列号"
        ><el-tag type="info">{{ updateFormData.sn?.replaceAll("-", "") }}</el-tag></el-form-item
      >
    </el-form>
  </el-card>
  <base-dialog @confirm="submitCb()" width="500px" :title="'系统升级'" v-model="showHandle">
    <el-form v-loading="loading">
      <el-form-item label="">
        <el-upload
          class="upload-demo"
          v-model:file-list="files"
          :limit="1"
          drag
          style="width: 100%"
          :auto-upload="false"
          ref="uploadRef"
        >
          <div class="h-box">
            <span>请上传升级包</span>
            <el-icon class="mg-l-sm"> <Upload /></el-icon>
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
  </base-dialog>
</template>

<style lang="scss" scoped></style>
